package pers.rcklos.myserver.biz.impl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.rcklos.myserver.biz.PaperBiz;
import pers.rcklos.myserver.dao.AdminDao;
import pers.rcklos.myserver.dao.PaperDao;
import pers.rcklos.myserver.dao.UserDao;
import pers.rcklos.myserver.entity.PaperItem;
import pers.rcklos.myserver.entity.SpUser;

@Service
public class PaperBizImpl implements PaperBiz {
	
	private PaperDao paperDao;
	private AdminDao adminDao;
	
	public PaperBizImpl(PaperDao paperDao) {
		// TODO Auto-generated constructor stub
		setPaperDao(paperDao);
	}
	
	@Override
	public ArrayList<PaperItem> queryAllPapers() {
		// TODO Auto-generated method stub
		return paperDao.queryAllPapers();
	}
	
	@Override
	public ArrayList<PaperItem> queryPageOfPapers(int skipPage, int pageSize) {
		// TODO Auto-generated method stub
		return paperDao.queryPageOfPapers(skipPage * pageSize, pageSize);
	}

	@Override
	public PaperItem addPaper(SpUser user, String title, String content) {
		// TODO Auto-generated method stub
		PaperItem paper = null;
		if(paperDao.insertPaper(user.getUid(), title, content) > 0) {
			paper = new PaperItem();
			paper.setUserId(user.getUid());
			paper.setTitle(title);
			paper.setContent(content);
		}
		return paper;
	}
	
	@Override
	public ArrayList<PaperItem> queryPapers(SpUser user) {
		// TODO Auto-generated method stub
		return paperDao.queryPapers(user.getUid());
	}
	
	@Override
	public PaperItem queryPaper(int paperId) {
		// TODO Auto-generated method stub
		return paperDao.queryPaper(paperId);
	}
	
	@Override
	public boolean deletePaper(SpUser user, int paperId) {
		// TODO Auto-generated method stub
		PaperItem paper = queryPaper(paperId);
		if(paper != null) {
			if(paper.getUserId() == user.getUid() || adminDao.queryAdmin(user.getUid()) != null) {
				return paperDao.deletePaper(paperId) > 0;
			}
		}
		return false;
	}
	
	@Autowired
	public void setPaperDao(PaperDao paperDao) {
		this.paperDao = paperDao;
	}
	
	@Autowired
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

}
